<?php
require_once(APPPATH.'libraries/newsobject.php');

/**
 * News Model
 */
class News_Model extends CI_Model {
	
	function __construct() {
		parent::__construct();
	}
	
	public function countNews($promotion = 0)
	{
		$query = "SELECT COUNT(DISTINCT id) as total FROM ".NEWS_TABLE." WHERE promotion=?";
		$data = $this->db->query($query, array($promotion));
		
		if ($data->num_rows() > 0)
		{
			$row = $data->row();
			return $row->total;
		}
		
		return 0;
	}
	
	public function getAllNews($offset = 0, $limit = NEWS_REC_LIMIT, $promotion = 0)
	{
		$newsArr = array();
		$query = "SELECT DISTINCT id, content, title, image, promotion "
		."FROM ".NEWS_TABLE." WHERE promotion=? LIMIT ?,?";
		
		$data = $this->db->query($query, array($promotion, $offset, $limit));
		
		foreach ($data->result() as $row)
		{
			$newsArr[] = new NewsObject($row->id, $row->title, $row->content, $row->image, $row->promotion);
		}
		
		return $newsArr;
	}
	
	public function getRelatedNews($limit = RELATED_ITEMS, $promotion = 0, $currentId = -1)
	{
		$offset = 0;
		$newsArr = array();
		$query = "SELECT DISTINCT id, content, title, image, promotion "
		."FROM ".NEWS_TABLE." WHERE promotion=? AND id <> ? ORDER BY id DESC LIMIT ?,?";
		
		$data = $this->db->query($query, array($promotion, $currentId, $offset, $limit));
		
		foreach ($data->result() as $row)
		{
			$newsArr[] = new NewsObject($row->id, $row->title, $row->content, $row->image, $row->promotion);
		}
		
		return $newsArr;
	}
	
	public function getNewsById($id)
	{
		$news = new NewsObject();
		
		$this->db->where('id', $id);
		$data = $this->db->get(NEWS_TABLE);
		
		if ($data->num_rows() > 0)
		{
			$row = $data->row();
			$news->setId($row->id);
			$news->setTitle($row->title);
			$news->setContent($row->content);
			$news->setImage($row->image);
			$news->setPromotion($row->promotion);
		}
		
		return $news;
	}
	
	public function insertNews($news)
	{
		$data = array(	"id"	=>	NULL,
						"title"		=>	$news->getTitle(),
						"content"	=>	$news->getContent(),
						"image"		=>	$news->getImage(),
						"promotion"	=>	$news->isPromotion());
		$this->db->trans_start();
		$this->db->insert(NEWS_TABLE, $data);
		$this->db->trans_complete();
		
		if ($this->db->trans_status() === FALSE)
			return INSERT_ERROR;
		return ERROR_NONE;
	}
	
	public function updateNews($id, $news)
	{
		$query = "UPDATE ".NEWS_TABLE." SET title=?, content=?, image=?, promotion=? WHERE id=?";
		
		$this->db->trans_start();
		$this->db->query($query, array($news->getTitle(), $news->getContent(), $news->getImage(), $news->isPromotion(), $id));
		$this->db->trans_complete();
		
		if ($this->db->trans_status() === FALSE)
			return UPDATE_ERROR;
		return ERROR_NONE;
	}
	
	public function deleteMultipleNews($ids)
	{
		$query = "";
		$size = count($ids);
		$query = "DELETE FROM ".NEWS_TABLE." WHERE id IN (";
		
		for($i = 0; $i < ($size - 1); $i++)
		{
			$query .= "?, ";
		}
		
		$query .= "?)";
		
		$this->db->trans_start();
		$this->db->query($query, $ids);
		$this->db->trans_complete();
		
		if ($this->db->trans_status() === FALSE)
			return DELETE_ERROR;
		return ERROR_NONE;
	}
}


?>